package io.characterStream.fileRorW.test;
import java.io.*;
/*
将E盘javaFileTest文件夹拷贝到桌面
 */
public class test5 {
    public static void main(String[] args) throws IOException {
        File src = new File("E:\\javaFiletest");
        File dest = new File("D:\\");
        copeDir(src, dest);
    }
    public static void copeDir(File src,File dest) throws IOException {
        //先创建新文件夹
        File newDir = new File(dest,src.getName());
        newDir.mkdir();
        //从数据源中获取数据
        File[] files = src.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                //复制文件
                FileInputStream fis = new FileInputStream(file);
                //路径拼接一下
                FileOutputStream fos = new FileOutputStream(new File(newDir,file.getName()));
                //拷贝文件
                int len;
                byte[] bys = new byte[1024];
                while((len=fis.read())!=-1){
                    fos.write(bys,0,len);
                }
                fis.close();
                fos.close();
            }else {
                //拷贝文件夹
                copeDir(file,newDir);
            }
        }
    }
}
